#!/usr/bin/env python
#
'''
Created on 2009-4-5

@author: Wang_Yuan
'''

import sys
import wx
import wx.lib.mixins.listctrl as listmix

from ExamReport_xrc import *
import WordDetailFrame



class ReportList(wx.ListCtrl, listmix.ListCtrlAutoWidthMixin, listmix.CheckListCtrlMixin):
    def __init__(self):
        listmix.CheckListCtrlMixin.__init__(self)
        listmix.ListCtrlAutoWidthMixin.__init__(self)
        self.setResizeColumn(0)

class ExamReportPanel(xrcExamReport):
    
    def __init__(self, parent, questionList):
        self.detailFrames = {}
        xrcExamReport.__init__(self, parent)
        self.__initCtrl()
        self.setQuestionList(questionList)
        
    def __initCtrl(self):
        self.resultList.__class__ = ReportList
        ReportList.__init__(self.resultList)
        imageList = self.resultList.GetImageList(wx.IMAGE_LIST_SMALL)
        if imageList is None:
            imageList = wx.ImageList(16, 16)
        self.correctIcon = imageList.Add(wx.Bitmap('ui/img/icon_tick.gif', type=wx.BITMAP_TYPE_GIF))
        self.wrongIcon = imageList.Add(wx.Bitmap('ui/img/icon_cross.gif', type=wx.BITMAP_TYPE_GIF))
        self.resultList.SetImageList(imageList, wx.IMAGE_LIST_SMALL)
        
    def setQuestionList(self, questionList):
        self.questionList = questionList
        self.resultList.ClearAll()
        self.resultList.InsertColumn(0, 'words')
        item = wx.ListItem()
        item.SetMask(wx.LIST_MASK_TEXT|wx.LIST_MASK_IMAGE)
        for question in questionList[1:]:
            word = question.wordData.word
            index = self.resultList.InsertStringItem(sys.maxint, word)
            while len(question.oxRecords) + 1 > self.resultList.GetColumnCount():
                i = self.resultList.InsertColumn(self.resultList.GetColumnCount(), 'result')
            if question.oxRecords:
                self.resultList.CheckItem(index)
            for i, record in zip(xrange(len(question.oxRecords)), question.oxRecords):
                self.resultList.SetItemColumnImage(index, i+1, 
                                                   self.correctIcon if record else self.wrongIcon)
            if index % 2:
                self.resultList.SetItemBackgroundColour(index, wx.Color(red=0xee, green=0xee, blue=0xee))
        for i in xrange(self.resultList.GetColumnCount() - 1):
            self.resultList.SetColumnWidth(i+1, 18)

    def OnList_item_selected_resultList(self, evt):
        index = evt.GetIndex()
        question = self.questionList[index + 1]
        wordData = question.wordData
        frame = self.detailFrames.get(wordData.word)
        if not frame:
            frame = WordDetailFrame.WordDetailFrame(self, wordData)
            self.detailFrames[wordData.word] = frame
        size = frame.GetSize()
        mPos = self.resultList.ClientToScreen(self.resultList.GetItemPosition(index))
        print mPos
        pos = (mPos[0] + self.GetSize()[0], max(0, mPos[1] - size[1]))
        frame.SetPosition(pos)
        frame.Show(True)
        frame.SetFocus()

class ExamReportDialog(wx.Dialog):
    
    def __init__(
            self, parent, questionList, ID=wx.ID_ANY, title='Exam Result', 
            style=wx.DEFAULT_DIALOG_STYLE|wx.RESIZE_BORDER,
            ):
        '''
        Constructor
        '''
        
        wx.Dialog.__init__(self, parent, ID, title, style=style)
        sizer = wx.BoxSizer(wx.VERTICAL)
        panel = ExamReportPanel(self, questionList)
        self.SetBackgroundColour(panel.resultList.GetBackgroundColour())
        sizer.Add(panel, proportion=1, flag=wx.ALL|wx.EXPAND, border=10)
        
        btnsizer = wx.StdDialogButtonSizer()
        btn = wx.Button(self, wx.ID_OK)
        btn.SetDefault()
        sizer.Add(btn, proportion=0, flag=wx.ALIGN_CENTER|wx.ALL, border=10)
        
        self.SetSizer(sizer)
        sizer.Fit(self)
        panel.resultList.resizeColumn(18)
        
